package com.terrynoya.fight.stateMachine.controllers
{
	import com.terrynoya.fight.char.Character;
	import com.terrynoya.fight.evaluation.Node;
	import com.terrynoya.fight.stateMachine.StateController;
	
	public class VarSet extends StateController
	{
		public static const SYSVAR_REG:RegExp = /sysvar\((\d+)\)/;
		public static const VAR_REG:RegExp = /var\((\d+)\)/;
		
		public function VarSet(name:String)
		{
			super(name);
		}
		
		override public function run(character:Character):void
		{
			var keys:Array = this.expressions.keys;
			for (var i:int = 0; i < keys.length; i++) 
			{
				var key:String = keys[i];
				var matches:Array;
				var index:int
				var rlt:int;
				var reg:RegExp;
				var isSystem:Boolean;
				if(key.indexOf("sysvar") != -1)
				{
					reg = SYSVAR_REG;
					isSystem = true;
//					matches = key.match(SYSVAR_REG);
//					index = matches[1];
//					rlt = this.getExpression(key).excute(character);
//					character.variables.setInteger(index,true,rlt);								
				}
				else
				{
					reg = VAR_REG;
					isSystem = false;
//					matches = key.match(VAR_REG);
//					index = matches[1];
//					rlt = this.getExpression(key).excute(character);
//					character.variables.setInteger(index,false,rlt);
				}
				matches = key.match(reg);
				index = matches[1];
				rlt = int(this.getExpression(key).excute(character));
				character.variables.setInteger(index,isSystem,rlt);
			}
		} 
	}
}